package cc.zy.base.businesses.controller;

import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.ModuleType;
import cc.zy.base.common.entity.OssProperties;
import cc.zy.base.common.utils.OssUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 第4组-文件上传
 * @date 2021-01-30 11:03:36
 */
@Slf4j
@RestController
public class OssController {

    @Autowired
    private OssProperties ossProperties;

    @ApiOperation(value = "文件上传")
    @PostMapping("upload")
    public FebsResponse upload(@ApiParam(value = "文件",required = true) @RequestParam(value = "file") MultipartFile file)throws Exception{
        //module可以选择IMG_PHOTO、IMG_IDEN、IMG_CHANGE、IMG_CERTIFICATE
        log.debug("正在上传文件");

        String uploadUrl= null;
        try {
            uploadUrl = OssUtil.upload(file,ossProperties, ModuleType.IMG_PHOTO.getPath());
            log.debug(String.format("uploadUrl = %s",uploadUrl));
        } catch (Exception e) {
            log.error(String.format("OSS上传异常 = %s", e));
            return new FebsResponse().fail().message("文件上传失败");
        }

        return new FebsResponse().success().message("文件上传成功").put("url",uploadUrl);
    }
}